<template>
  <el-dialog
    :visible.sync="visible"
    title="回访"
    width="60%"
    :before-close="handleClose"
  >
    <el-form
      :model="returnVisitForm"
      :rules="returnVisitFormRules"
      ref="returnVisitFormRef"
      label-width="110px"
      label-position="left"
    >
      <div class="top-div">
        <span class="top-code">{{ serviceProductName }}</span>
        <span class="top-status">{{ phone }}</span>
      </div>
      <el-form-item label="实际完成服务" prop="product_id">
        <el-select
          v-model="returnVisitForm.product_id"
          placeholder="请选择"
          filterable
          clearable
        >
          <el-option
            v-for="item in products"
            :key="item.id"
            :label="item.name + '【' + item.id + '】'"
            :value="
              typeof returnVisitForm.product_id == 'string'
                ? item.id + ''
                : item.id
            "
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="回访时间" prop="return_visit_date">
        <el-date-picker
          v-model="returnVisitForm.return_visit_date"
          type="datetime"
          placeholder="选择日期时间"
          value-format="yyyy-MM-dd HH:mm:ss"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="回访备注" prop="remark">
        <el-input
          v-model="returnVisitForm.remark"
          placeholder="请输入回访备注"
          type="textarea"
          show-word-limit
          maxlength="500"
        ></el-input>
      </el-form-item>
      <el-form-item label="回访结果" prop="result">
        <el-radio v-model="returnVisitForm.result" label="2">
          继续回访
        </el-radio>
        <el-radio v-model="returnVisitForm.result" label="1">
          回访完成
        </el-radio>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">取 消</el-button>
      <el-button
        class="theme-background-color"
        @click="saveDispatchInfo"
        :disabled="isDisabled"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    returnVisitDialogVisible: {
      type: Boolean,
      default: function () {
        return {}
      }
    },
    serviceProductName: {
      type: String,
      default: function () {
        return {}
      }
    },
    serviceProductId: {
      type: Number,
      default: function () {
        return {}
      }
    },
    phone: {
      type: String,
      default: function () {
        return {}
      }
    },
    orderId: {
      type: Number,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      visible: false,
      products: [],
      returnVisitForm: {
        product_id: this.serviceProductId == 0 ? '' : this.serviceProductId,
        return_visit_date: this.getFormatDatetime(new Date()),
        remark: '',
        result: '1'
      },
      returnVisitFormRules: {
        product_id: [{ required: true, message: '请选择', trigger: 'change' }],
        return_visit_date: [
          { required: true, message: '请选择', trigger: 'change' }
        ],
        remark: [{ required: true, message: '请输入', trigger: 'blur' }],
        result: [{ required: true, message: '请选择', trigger: 'change' }]
      },
      isDisabled: false
    }
  },
  methods: {
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.$emit('update:returnVisitDialogVisible', false)
          done()
        })
        .catch((_) => {})
    },
    closeDialog() {
      this.$emit('update:returnVisitDialogVisible', false)
    },
    saveDispatchInfo() {
      this.$refs.returnVisitFormRef.validate(async (valid) => {
        if (!valid) return
        this.returnVisitForm.order_booking_id = this.orderId
        console.log(this.returnVisitForm)

        this.isDisabled = true
        const { data: res } = await this.$http.post(
          '/admin/order_booking/returnVisit',
          this.returnVisitForm
        )
        this.isDisabled = false
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('操作成功')
        this.visible = false
        this.$emit('update:returnVisitDialogVisible', false)
        this.$parent.getDetailData()
      })
    },
    async getSelectData() {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'product',
        type: 1
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.products = res.data.product
    }
  },
  watch: {
    returnVisitDialogVisible() {
      this.visible = this.returnVisitDialogVisible
      if (this.visible) {
        this.getSelectData()
      } else {
        this.$refs.returnVisitFormRef.resetFields()
      }
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 80%;
  margin-left: 15px;
  .el-select {
    width: 100%;
  }
}
.top-div {
  margin-bottom: 10px;
  .top-code {
    font-family: '微软雅黑 Bold', '微软雅黑', sans-serif;
    font-weight: 700;
    font-size: 16px;
  }
  .top-status {
    font-family: '微软雅黑 Bold', '微软雅黑', sans-serif;
    font-weight: 700;
    font-style: normal;
    font-size: 16px;
    color: #009688;
    line-height: normal;
    margin-left: 30px;
  }
}
</style>
